package com.lib.common.dialog_fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.google.android.material.textfield.TextInputEditText;
import com.lib.common.R;
import com.lib.utils.RegexUtils;

@SuppressLint("ValidFragment")
public class FindPwdDialogFragment extends BaseDialogFragment implements View.OnClickListener {
    private TextInputEditText mAccount;
    private TextInputEditText mVCode;
    private TextInputEditText mPwd;
    private TextView mSendVerifyCode;
    private View mBtn;

    private VCodeHandler mHandler;

    public FindPwdDialogFragment() {

    }

    @Override
    @SuppressLint("InflateParams")
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_fragment_find_pwd, null);

        mAccount = mView.findViewById(R.id.account);
        mVCode = mView.findViewById(R.id.v_code);
        mPwd = mView.findViewById(R.id.pwd);
        mSendVerifyCode = mView.findViewById(R.id.sendVerifyCode);

        mSendVerifyCode.setOnClickListener(this);
        mBtn = mView.findViewById(R.id.btn_find);
        mBtn.setOnClickListener(this);

        mHandler = new VCodeHandler(this, mSendVerifyCode);
    }

    @Override
    public void onClick(View v) {
        FragmentActivity activity = getActivity();

        Editable text = mAccount.getText();
        String str;
        if (text == null || TextUtils.isEmpty(str = text.toString()) || !RegexUtils.isMobileExact(str)) {
            Toast.makeText(activity, getString(R.string.please_check_phone), Toast.LENGTH_SHORT).show();
            return;
        }
        int i = v.getId();
        if (i == R.id.sendVerifyCode) {

            mSendVerifyCode.setEnabled(false);
            mHandler.sendEmptyMessage(VCodeHandler.MSG_SHOW_WAIT);
            Toast.makeText(activity, "短信发送成功，请注意查看", Toast.LENGTH_SHORT).show();
        } else if (i == R.id.btn_find) {
            Editable v_code = mVCode.getText();
            String codeStr;
            if (v_code == null || TextUtils.isEmpty(codeStr = v_code.toString())) {
                Toast.makeText(activity, getString(R.string.no_empty_v_code), Toast.LENGTH_SHORT).show();
                return;
            }
            Editable pwd = mPwd.getText();
            String pwdStr;
            if (pwd == null || TextUtils.isEmpty(pwdStr = pwd.toString()) || !pwdStr.matches("[a-zA-Z0-9_]{6,16}")) {
                Toast.makeText(activity, getString(R.string.no_standard_pwd), Toast.LENGTH_SHORT).show();
                return;
            }
            mBtn.setEnabled(false);
        }
    }
}